﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;


namespace WebLoginner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ap = new Ession.AutoPost();
            if (checkBox1.Checked)
            {
                button1_Click(null, null);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Process.Start("notepad", textBox2.Text);
        }
        bool autoRun = false;
        private Ession.AutoPost ap;
        private Thread th;
        private Thread th2;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                autoRun = checkBox1.Checked;
                toolStripStatusLabel1.Text = done ? "done" : "running";
                loginUrl = textBox1.Text;
                urlAfterLogin = textBox3.Text;
                filelocation = textBox2.Text;
                th = new Thread(
                      GetSettingsInfoAndLogin
                   );
                th.Start();
                th2 = new Thread(UpdateState);
                th2.Start();
            }
            catch (Exception)
            {
                return;
            }
        }

        private string urlAfterLogin;
        private string currentUser;
        private string loginUrl;
        private string result;
        private string filelocation;
        private bool done = false;
        private void GetSettingsInfoAndLogin()
        {
            string postHeader;
            string postData = "";
            using (var sr = new StreamReader(new FileStream(filelocation, FileMode.Open, FileAccess.Read)))
            {
                postHeader = sr.ReadLine();

                currentUser = sr.ReadLine();
                while (!string.IsNullOrEmpty(currentUser))
                {
                    postData = postHeader + currentUser;
                    result = ap.PostData(loginUrl, postData, Encoding.Default);
                    if (!string.IsNullOrEmpty(urlAfterLogin))
                    {
                        result = ap.GetHtml(urlAfterLogin);
                    }
                    currentUser = sr.ReadLine();
                }
                done = true;
                if (autoRun)
                {
                    this.Invoke(

                         (Action)(
                     () =>
                     {
                         this.Close();
                     }
                                 ))
                     ;
                }
            }

        }

        private void UpdateState()
        {
            while (true)
            {
                try
                {
                    this.Invoke(

                            (Action)(
                        () =>
                        {
                            richTextBox1.Text = result;
                            toolStripStatusLabel1.Text = done ? "done" : "running";
                            toolStripStatusLabel1.Text += "," + currentUser;
                        }
                                    ))
                        ;
                }
                catch (Exception)
                {


                }

                Thread.Sleep(300);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            WebLoginner.Properties.Settings.Default.Save();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
